package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.activity.CalendarMyActivityActivity;
import com.badibadi.adapter.ActivityMyActivityAdapter;
import com.badibadi.adapter.LinkActivityAdapter;
import com.badibadi.adapter.Popwindows_Adapter;
import com.badibadi.fragment.ActivityMyActivityFragment0.GetList;
import com.badibadi.fragment.ActivityMyActivityFragment0.getFragmentData;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.libjph.util.StringUtil;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 相关活动：创建时间 2014-10-10
 * 
 * @author Administrator
 * 
 */
public class linkActivityFragment extends BaseFragment implements
		IXListViewListener {
	protected static final String TAG = "linkActivityFragment";
	static linkActivityFragment fragment0;
	private LinkActivityAdapter x1Adapter;
	private XListView xListView1;
	private PopupWindow popup = null;

	// private int Type;// 显示类型
	private List<Activity_Overview_Model> activity_Overview_Models;
	private int languageType = 0;
	private MySharePreferences preferences;

	private Results results;
	private List<Activity_Overview_Model> list;

	/*
	 * 相关活动---POST
	 * 
	 * @param $uid //浏览者用户ID----测试ID：2
	 * 
	 * @param $actId //活动ID-----测试ID：3
	 * 
	 * @param $languageType ;（必需）//name为中文简体，tw_name 中文繁体 en_name 英文
	 * 
	 * @param $page ; //（必需）第几页
	 * 
	 * @param $pageNum ;//（必需）每页显示条数
	 */
	private String uid = "2";
	private String actId = "3";
	private int Page;// 当前显示数据页数
	private int pageNum = 4;// 每页显示条数

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));
					xListView1.stopRefresh();
					xListView1.stopLoadMore();
					xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
				} catch (Exception e) {
					// TODO: handle exception
				}
			
				break;
			case 2:
				try {
					Utils.ExitPrgress(getActivity());
					activity_Overview_Models.addAll(list);
					x1Adapter.notifyDataSetChanged();
					xListView1.stopRefresh();
					xListView1.stopLoadMore();
					xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
				} catch (Exception e) {
					// TODO: handle exception
				}
			
				break;
			case 3:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_xa10));
					xListView1.stopRefresh();
					xListView1.stopLoadMore();
					xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				break;

			default:
				break;
			}
		}
	};
	private LinearLayout acticity_overview0_btn_type;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		actId = getArguments().getString("actId");
		uid = Utils.getUid(getActivity());
		// uid = getArguments().getString("uid", "26");

	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		final View view = inflater.inflate(
				R.layout.activity_linkactivitylayout, null);
		Page = 0;
		activity_Overview_Models = new ArrayList<Activity_Overview_Model>();
		x1Adapter = new LinkActivityAdapter(activity_Overview_Models,
				getActivity(), 0);
		preferences = new MySharePreferences(getActivity(), "language");
		languageType = preferences.get("language", 0);
		InitMyXListView1(view);
		// Activity(Type, Page);

		return view;
	}

	/**
	 * 相关活动---POST
	 * 
	 * @param $uid
	 *            //浏览者用户ID----测试ID：2
	 * @param $actId
	 *            //活动ID-----测试ID：3
	 * @param $languageType
	 *            ;（必需）//name为中文简体，tw_name 中文繁体 en_name 英文
	 * @param $page
	 *            ; //（必需）第几页
	 * @param $pageNum
	 *            ;//（必需）每页显示条数
	 */
	private void LinkActivity(final String uid, final String actId,
			final int page, final int pageNum) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<Activity_Overview_Model>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("actId", actId);
				map.put("languageType",
						Dialog.getSystemLanguageTypegrzx(getActivity()));
				map.put("page", "" + page);
				map.put("pageNum", "" + pageNum);
				String actcontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "activity/linkActivity");
//				   
				if (actcontent != null) {
//					   
					results = Utils.checkResult_NNN(getActivity(), actcontent);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(),
									Activity_Overview_Model.class);
							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
							mHandler.sendEmptyMessage(3);
						}
					} else {
						if (Page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						mHandler.sendEmptyMessage(3);
					}
				} else {
					if (Page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	public void InitMyXListView1(View view) {
		xListView1 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView2);
		xListView1.setDividerHeight(10);
		xListView1.setPadding(10, 0, 10, 10);
		xListView1.setXListViewListener(this);
		xListView1.setPullLoadEnable(true);
		xListView1.setPullRefreshEnable(true);
		xListView1.setAdapter(x1Adapter);
		// xListView1.startRefresh();
		xListView1.startLoadMore();
	}

	public static linkActivityFragment getInstance() {
		if (fragment0 == null) {
			return new linkActivityFragment();
		}
		return fragment0;
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Page = 1;
		activity_Overview_Models = new ArrayList<Activity_Overview_Model>();
		LinkActivity(uid, actId, Page, pageNum);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		LinkActivity(uid, actId, ++Page, pageNum);
	}

	/**
	 * 获取list中的数据（接口回调）
	 * 
	 * @param list
	 */
	public void getList(GetList list) {
		list.getList(activity_Overview_Models);
	}

	public interface getFragmentData {
		// 是否返回到上一级returnSearch
		void getIsReturnUp(boolean isReturn);
	}

	public interface GetList {
		void getList(List<Activity_Overview_Model> list);
	}
}
